//
//  DHHomeRecommendViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/4/25.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHHomeRecommendViewController.h"
#import "DHHomeRecommendCollectionCell.h"
#import "DHS2_ProductDetailController.h"

@interface DHHomeRecommendViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *itemArr; //数据

@end

@implementation DHHomeRecommendViewController

- (NSMutableArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSMutableArray array];
    }
    return _itemArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.titleLabel setText:@"推荐服饰"];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHHomeRecommendCollectionCell" bundle:nil] forCellWithReuseIdentifier:kDHHomeRecommendCollectionCell];
    self.collectionView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.collectionView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    
    [self loadData];
}

- (void)loadData{
    [[DHNetAPIClient shareDHNetAPIClient] request_recommend_productsAndBlock:^(id data, NSError *error) {
        [self.collectionView.mj_header endRefreshing];
        if (data) {
            self.itemArr = data;
            [self.collectionView reloadData];
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHHomeRecommendCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHHomeRecommendCollectionCell forIndexPath:indexPath];
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    [cell updateCell:model];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return (CGSize){(kScreenWidth - 16 - 8)/2,280};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    DHS2_ProductDetailController *vc = [[DHS2_ProductDetailController alloc] initWithProduct:model];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
